package com.zp.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.system.entity.bo.SysBaseDictDataBo;
import com.zp.system.entity.vo.SysBaseDictDataVo;
import com.zp.system.service.ISysBaseDictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 系统平台字典控制器
 * 前端访问路由地址为:/system/baseData
 *
 * @author zhang peng
 * @date 2023-01-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/baseData")
public class SysBaseDictDataController extends BaseController {

    private final ISysBaseDictDataService iSysBaseDictDataService;

    /**
     * 查询系统平台字典列表 (分页)
     */
    @SaCheckPermission("system:baseData:list")
    @GetMapping("/selectPage")
    public TableDataInfo<SysBaseDictDataVo> selectPage (SysBaseDictDataBo bo, PageQuery pageQuery) {
        return iSysBaseDictDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询系统平台字典列表 (不分页)
     */
    @SaCheckPermission("system:baseData:list")
    @GetMapping("/list")
    public R<List<SysBaseDictDataVo>> list (SysBaseDictDataBo bo) {
        List<SysBaseDictDataVo> list = iSysBaseDictDataService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出系统平台字典列表
     */
    @SaCheckPermission("system:baseData:export")
    @Log(title = "系统平台字典", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (SysBaseDictDataBo bo, HttpServletResponse response) {
        List<SysBaseDictDataVo> list = iSysBaseDictDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "系统平台字典", SysBaseDictDataVo.class, response);
    }

    /**
     * 获取系统平台字典详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:baseData:query")
    @GetMapping("/{id}")
    public R<SysBaseDictDataVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iSysBaseDictDataService.queryById(id));
    }

    /**
     * 新增系统平台字典
     */
    @SaCheckPermission("system:baseData:add")
    @Log(title = "系统平台字典", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody SysBaseDictDataBo bo) {
        return toAjax(iSysBaseDictDataService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改系统平台字典
     */
    @SaCheckPermission("system:baseData:edit")
    @Log(title = "系统平台字典", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody SysBaseDictDataBo bo) {
        return toAjax(iSysBaseDictDataService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除系统平台字典
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:baseData:remove")
    @Log(title = "系统平台字典", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        return toAjax(iSysBaseDictDataService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
